package com.bailiban.aigou.admin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bailiban.aigou.admin.common.BaseAction;
import com.bailiban.aigou.admin.entity.Classification;
import com.bailiban.aigou.admin.entity.Product;
import com.bailiban.aigou.admin.service.ICommodityclassificationService;
import com.bailiban.aigou.admin.service.IProductService;
import com.bailiban.aigou.admin.utils.DateUtils;
import com.bailiban.aigou.admin.utils.PageModel;
import com.bailiban.aigou.admin.utils.UUIDUtils;


@Controller
@Scope("prototype")
@Namespace("/admin/product")
@Results({
    @Result(name = "error", location = "/WEB-INF/jsp/aigou/500.jsp"),
	@Result(name = "login", location = "/WEB-INF/jsp/aigou/admin/login/login.jsp"),
	@Result(name = "close", location = "/WEB-INF/jsp/aigou/success.jsp")
})
public class ProductAction extends BaseAction {
	@Autowired
	private IProductService productService;
	@Autowired
    private ICommodityclassificationService commodityclassificationService;
	/**
	 * 类名：ProductAction
	 * 类型：@Controller层  action
	 * 功能描述：商品管理 商品维护
	 * **/
	private static final long serialVersionUID = 1L;
	
	
	/**
	 * list分类数据显示
	 * @return
	 */
	 @Action(value="list",results={ @Result( name="success" ,location ="/WEB-INF/jsp/aigou/admin/product/list.jsp")})
	 public String list(){
		    HttpServletRequest request = ServletActionContext.getRequest();
		    super.setPageNo(request.getParameter("pageNo"));
			super.setPageSize(request.getParameter("pageSize"));
		    String productName = request.getParameter("productName");
			String productType = request.getParameter("productType");
			System.out.println("ss"+productName+productType+"sss");
			request.setAttribute("productName", productName);
			request.setAttribute("type", productType);
			List<Classification> productTypes = commodityclassificationService.getAllProductTypes();
			  /***/
			
		    PageModel<Product> pageModel =productService.getProductPages(super.getPageNo(), super.getPageSize());
		    System.out.println("pageModel"+pageModel.getList().get(0).getProductName());
		    request.setAttribute("productTypes", productTypes);
		    request.setAttribute("productPages", pageModel);
		 
		return SUCCESS;
		 
	 }
	 /**
	  * 去添加页面
	  * @return
	  */																
	 @Action(value="addPage",results={ @Result( name=SUCCESS ,location ="/WEB-INF/jsp/aigou/admin/product/add.jsp")})
	public String addPage(){
		 HttpServletRequest request = ServletActionContext.getRequest();
		 List<Classification> classifications = commodityclassificationService.getAllProductTypes();
		  request.setAttribute("productTypes", classifications);
		return SUCCESS;
		
	}
	 @Action(value="add")
	 public String add(){
		    HttpServletRequest request = ServletActionContext.getRequest();
		    String productName = request.getParameter("productName");
			String productImage = request.getParameter("productImage");
			double price = Double.valueOf(request.getParameter("price"));
			String Classification_id = request.getParameter("productType");
			String productDesc = request.getParameter("productDesc");
			
			Classification classification2 = commodityclassificationService.getProductTypeById(Classification_id);
			
			Product product = new Product();
			product.setId(UUIDUtils.getId());
			product.setProductName(productName);
			product.setProductImage(productImage);
			product.setPrice(price);
			product.setClassification(classification2);
			product.setProductDesc(productDesc);
			product.setCreateTime(DateUtils.getDateTimeString());
			
			productService.addProduct(product);
			request.setAttribute("url", "admin/product/list");
		return "close";
		 
	 }
	 /**
	  * 去到更新页面
	  * @return
	  */
	 @Action(value="updatePage",results={@Result(name=SUCCESS,location="/WEB-INF/jsp/aigou/admin/product/update.jsp")})
	 public String  updatePage(){
		    HttpServletRequest request = ServletActionContext.getRequest();
		    String id = request.getParameter("id");
		    List<Classification> productTypes = commodityclassificationService.getAllProductTypes();
			request.setAttribute("productTypes", productTypes);
			
			Product product = productService.getProductById(id);
			request.setAttribute("product", product);
		return SUCCESS;
		 
	 }
	 /**
	  * 更新商品
	  * @return
	  */
	 @Action(value="update")
	 public String update(){
		  HttpServletRequest request = ServletActionContext.getRequest();
		    String id = request.getParameter("id");
			String productName = request.getParameter("productName");
			String productImage = request.getParameter("productImage");
			double price = Double.valueOf(request.getParameter("price"));
			String Classification_id = request.getParameter("productType");
			String productDesc = request.getParameter("productDesc");

			Product product = productService.getProductById(id);
			product.setProductName(productName);
			product.setProductImage(productImage);
			product.setPrice(price);
			product.setProductDesc(productDesc);
			
			Classification classification2 = commodityclassificationService.getProductTypeById(Classification_id);
			product.setClassification(classification2);
			productService.updateProduct(product);
			
			request.setAttribute("url", "admin/product/list");
		return "close";
		 
	 }
	 /**
	  * 删除商品
	  * @return
	  */
	 @Action(value="delete")
	 public String delete(){
		  HttpServletRequest request = ServletActionContext.getRequest();
		  String id = request.getParameter("id");
		  productService.deleteProductById(id);
		  request.setAttribute("url", "admin/product/list");
			return "close";
	 }
}
